/*******************************************************************************
*
* Copyright (C) 2013 - 2015, Jeffery Jiang, <china_jeffery@163.com>.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*
* Expect bugs
*
* Please use and enjoy. Please let me know of any bugs/mods/improvements
* that you have found/implemented and I will fix/incorporate them into this
* file.
*
*******************************************************************************/

#include "libipc.h"
#include "WinPipe.h"
#include "IPCManager.h"
#include "Sender.h"

LIBIPC_API H_IPC IPC_Init(void)
{
    WinPipe *pWinPipe = new WinPipe();
    return pWinPipe;
}

LIBIPC_API BOOL IPC_Create(H_IPC ipc, LPCTSTR szIPCName, PFN_LISTENING_CALLBACK pfnCallback)
{
    WinPipe *pWinPipe = reinterpret_cast<WinPipe *>(ipc);

    if(NULL == pWinPipe)
        return FALSE;

    return pWinPipe->StartListen(szIPCName, pfnCallback);
}

LIBIPC_API BOOL IPC_Release(H_IPC ipc)
{
    WinPipe *pWinPipe = reinterpret_cast<WinPipe *>(ipc);

    if(!pWinPipe)
        return FALSE;

    pWinPipe->StopListen();
    delete pWinPipe;
    pWinPipe = NULL;

    return TRUE;
}

LIBIPC_API LONG IPC_SendMessage(LPCTSTR szIPCName, LPVOID pvMsg, DWORD dwMsgSize, LPVOID pvResponse, DWORD dwResponseSize, DWORD dwTimeout)
{
    return SendIPCMessage(szIPCName, pvMsg, dwMsgSize, pvResponse, dwResponseSize, dwTimeout);
}

LIBIPC_API BOOL IPC_PostMessage(LPCTSTR szIPCName, LPVOID pvMsg, DWORD dwMsgSize)
{
    return PostIPCMessage(szIPCName, pvMsg, dwMsgSize);
}

LIBIPC_API DWORD GetIPCName(H_IPC ipc, LPTSTR szName, DWORD dwMaxCount)
{
    WinPipe *pWinPipe = reinterpret_cast<WinPipe *>(ipc);

    if(!pWinPipe)
        return 0;

    return pWinPipe->GetIPCName(szName, dwMaxCount);
}

LIBIPC_API DWORD GetLocalhostIPCList(IPCDetail *details, DWORD dwMaxItem)
{
    return QueryIPCList(details, dwMaxItem);
}

LIBIPC_API BOOL IPC_SetBufferSize(H_IPC ipc, DWORD dwSize)
{
    WinPipe *pWinPipe = reinterpret_cast<WinPipe *>(ipc);

    if(!pWinPipe)
        return FALSE;

    return pWinPipe->SetBufferSize(dwSize);
}

LIBIPC_API BOOL IPC_SetMaxConcurrent(H_IPC ipc, int concurrent)
{
    WinPipe *pWinPipe = reinterpret_cast<WinPipe *>(ipc);

    if(!pWinPipe)
        return FALSE;

    return pWinPipe->SetMaxConcurrent(concurrent);
}


